home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / print / bjfilt.arj / FINDSTR.C < prev    next >
Text File  |  1994-02-03  |  1KB  |  47 lines

  1. /* findstr( char * str1, int n1, char *str2, int n2)
  2. * find if str1 overlaps str2 - ie if it is contained in str2, or if string 1 overlaps the end of 
  3. * On error ( strings of negative length) or no match,  returns -1, 
  4. *otherwise return the length into string 2 that the match (or partial match) occurs.
  5. *Copyright (C) W. G. Unruh Jan 29 1994
  6. */
  7.  
  8. int findstr( char * str1, int n1, char *str2, int n2)
  9.  
  10. {
  11.       char c;
  12.        int i,j,k;
  13.       c = str1[0];
  14.   if(n1<=0 || n2<=0) return(-1);
  15.   if(n2>n1)
  16.    {
  17.        for(i=0;i<n2-n1+1;i++)
  18.       { 
  19.            if( c == str2[i])                                           /* First character matches, check the rest*/
  20.              { for(j=1;j<n1;j++)
  21.                    { if ( str2[i+j]!=str1[j]) break; 
  22.                    }
  23.                 if(j==n1) return (i);
  24.              }
  25.                     /* Have we found a match for all of them?*/
  26.        }
  27.     }
  28.             /* No complete matches- Now check the end of the string */
  29.  
  30. /* End of String */
  31.   k=(n2>n1?n2:n1-1);
  32.        { 
  33.            for (i=k-n1+1 ; i<n2;i++)
  34.               {
  35.                       if(c==str2[i])
  36.                         {
  37.                          for(j=1;j<n2-i;j++)
  38.                             { if(str1[j]!= str2[i+j]) break;
  39.                             }
  40.                          if (j==n2-i) return (i);
  41.                         }
  42.                }
  43.          }
  44.    
  45.           return(-1);
  46. }
  47.